package com.example.mntz.controller;

import com.example.mntz.model.Collectionpage;
import com.example.mntz.model.User;
import com.example.mntz.model.Yjbd;
import com.example.mntz.service.CmService;
import com.example.mntz.service.CollectionPageService;
import com.example.mntz.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class HelloController {
    @Autowired
    LoginService login;
    @Autowired
    CollectionPageService servicePage;
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    // 发送post请求，代替了RequestMapping（value="/user/login", method="post"）
    @PostMapping(value = "/index")
    // 对登录请求判断request的参数值，并存放在map中
    public String login(User user, HttpSession session) {
        // 我们判断，如果登录名不是空，并且，密码是 123456 就登录成功（暂不涉及数据库）
        User u = login.login(user);
        if (u!=null) {
            // 登录成功，就跳转到下一个页面
            session.setAttribute("session_user",u);
            return "index";
        } else {
            // 登录失败，刷新本登录页
            return "login";
        }
    }

    @GetMapping("menu")
    public ResponseEntity<List> menu(HttpSession session,int top){
        User user = (User) session.getAttribute("session_user");
        List<Yjbd> yjbds = login.selectyjbds(user,top);
        return   ResponseEntity.status(200).body(yjbds);
    }
    @GetMapping("/welcome")
    public ModelAndView welcome(ModelAndView model ,HttpSession session){
        model.setViewName("welcome");
        List<Collectionpage> collectionpages = login.selectCollectionPage(((User)session.getAttribute("session_user")).getuId());
        model.addObject("collectionpage",collectionpages);
        return model;
    }
    @GetMapping("/CollectionPage")
    @ResponseBody
    public String CollectionPage(String page,HttpSession session){
        User u = (User)session.getAttribute("session_user");
        String coll = servicePage.havePage(page,u.getuId());
        return coll;
    }
    /*收藏页面*/
    @PostMapping("/CollectionNewPage")
    @ResponseBody
    public String CollectionNewPage(Collectionpage page,HttpSession session){
        User u = (User)session.getAttribute("session_user");
        Integer i = u.getuId();
        String col = servicePage.havePage(page.getPage(),i);
        if(col!="wsc"){
            servicePage.removePage(page.getPage(),i);

        }else {
            page.setUId(i);
            servicePage.insertPage(page);
        }
        return col;
    }
    @RequestMapping("/zc")
    public String zc(){
        return "add-user";
    }
    @RequestMapping("/insertUser")
    @ResponseBody
    public String insertUser(User user){
        login.insertUser(user);
        return "成功";
    }
}